package com.idc.service.impl;

import com.idc.mapper.SpringSysUserMapper;
import com.idc.vo.SpringSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.stream.Collectors;



@Service
public class SpringUserDetailService implements UserDetailsService {

	@Autowired
	private SpringSysUserMapper springSysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SpringSysUser springSysUser = springSysUserMapper.findSpringSysUserByUserName(username);

		Set<GrantedAuthority> authorities = springSysUser.getPermissions().stream()
				.map(x -> new SimpleGrantedAuthority(x.getPermissionTag())).collect(Collectors.toSet());

		springSysUser.setAuthorities(authorities);
		// System.out.println(springSysUser);
		return springSysUser;
    }
}
